<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" width="650px" @close="close">
      <el-form ref="form" v-loading="loading" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="标签：" prop="name">
          <el-input v-model="form.name" type="text" maxlength="10" placeholder="请输入标签" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="loading" size="small" round @click="hide">取 消</el-button>
        <el-button :disabled="loading" size="small" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addTag } from '@/api/tag'

export default {
  data() {
    return {
      // 校验字段
      rules: {
        name: [
          {
            required: true,
            message: '请输入',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      title: '新增标签',
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      form: {
        name: ''
      }
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {
  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    /**
     * 弹框相关
     */
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form = {
        name: ''
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */

    /**
     * 按钮组
     */
    // 确 定
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          addTag(this.form).then(() => {
            this.$notify({
              title: '成功',
              message: '新增成功',
              type: 'success',
              duration: 2000,
              offset: 40
            })
            this.resolve && this.resolve()
            this.hide()
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }

    /**
     * 状态组
     */

    /**
     * 方法组
     */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
